//
//  CLSelectDiseaseDetailController.m
//  CareLink
//
//  Created by tanky on 16/8/19.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "CLSelectDiseaseDetailController.h"
#import "CLSelectDiseaseDetailView.h"
#import "MJRefresh.h"
#import "CLDiseaseDetailData.h"
#import "UTPinYinHelper.h"


@interface CLSelectDiseaseDetailController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

@property(nonatomic,strong)NSArray *diseaseDetailModels;
@property(nonatomic,strong)CLSelectDiseaseDetailView *detailView;
@property(nonatomic,strong)NSMutableArray *diseaseDetailNames;
@property(nonatomic,strong)NSArray *tmpNames;

@end

@implementation CLSelectDiseaseDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadDiseaseDetailData];
    
}

- (void)setupUI{
    self.title = @"疾病详情选择";
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    searchBar.placeholder = @"请输入查找内容";
    [self.view addSubview:searchBar];
    searchBar.delegate = self;
    CLSelectDiseaseDetailView *detailView = [CLSelectDiseaseDetailView new];
    
    [self.view addSubview:detailView];
    
    [detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(searchBar.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    detailView.delegate = self;
    detailView.dataSource = self;
    
    detailView.tableFooterView = [UIView new];
    detailView.backgroundColor = [UIColor colorWithRed:230 green:230 blue:230];
    
    detailView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 结束刷新
            [detailView.mj_header endRefreshing];
        });
    }];
    self.detailView = detailView;
}

- (void)loadDiseaseDetailData{
    int i = 0;
    if ([self.diseaseTypeparameter isEqualToString:@"肿瘤"]) {
        i = 1;
    }else if ([self.diseaseTypeparameter isEqualToString:@"血液病"]) {
        i = 2;
    }if ([self.diseaseTypeparameter isEqualToString:@"心脑血管病"]) {
        i = 3;
    }else if ([self.diseaseTypeparameter isEqualToString:@"神经系统"]) {
        i = 4;
    }else if ([self.diseaseTypeparameter isEqualToString:@"骨科病"]) {
        i = 5;
    }
    NSString *url = @"http://iosapi.itcast.cn/searchCI3List.json.php";
    NSDictionary *paramDic = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f",@"keyword":@"",@"ci1_id":@(i),@"page_size":@(15),@"page":@(1)};
    NSMutableArray *diseaseDetailNames = [NSMutableArray new];
    NSMutableArray *mArr = [NSMutableArray new];
    CLNetworkManager4JSON *netManager = [CLNetworkManager4JSON sharedManager];
    [netManager POST:url parameters:paramDic success:^(id responseObject) {
        NSArray *arr = [responseObject objectForKey:@"data"];
        for (NSDictionary *dict in arr) {
            CLDiseaseDetailData *diseaseDetailModel = [CLDiseaseDetailData yy_modelWithDictionary:dict];
            [mArr addObject:diseaseDetailModel];
            [diseaseDetailNames addObject:diseaseDetailModel.ci3_name];
        }
        self.diseaseDetailModels = mArr;
        self.tmpNames = diseaseDetailNames.copy;
        self.diseaseDetailNames = [NSMutableArray arrayWithArray:diseaseDetailNames.copy];
        [self.detailView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"数据错误: %@",error);
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.diseaseDetailNames.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"selectDiseaseDetail_cellif"];
    cell.textLabel.text = self.diseaseDetailNames[indexPath.row];
    return cell;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(nonnull NSString *)searchText
{
    NSLog(@"%@",searchText);
    NSLog(@"%@",self.tmpNames);
    NSString *strText = searchText;
    [self.diseaseDetailNames removeAllObjects];
    if (strText.length <= 0) {
        self.diseaseDetailNames = [NSMutableArray arrayWithArray:self.tmpNames];
        [self.detailView reloadData];
        return;
    }
    
    [self.tmpNames enumerateObjectsUsingBlock:^(NSString *strName, NSUInteger idx, BOOL *stop) {
        UTPinYinHelper *pinYinHelper = [UTPinYinHelper sharedPinYinHelper];
        if ([pinYinHelper isString:strName MatchsKey:strText IgnorCase:YES]) {
            [self.diseaseDetailNames addObject:strName];
        }
    }];
    
    [self.detailView reloadData];

}











- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.diseaseDetailBlock(self.diseaseDetailNames[indexPath.row]);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(NSMutableArray *)diseaseDetailNames
{
    if (!_diseaseDetailNames) {
        _diseaseDetailNames = [[NSMutableArray alloc]init];
    }
    
    return _diseaseDetailNames;
}

-(NSArray *)tmpNames
{
    if (!_tmpNames) {
        _tmpNames = [[NSArray alloc]init];
    }
    return _tmpNames;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
